package com.ruoyi.factorydata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.CustomerLevelData;
import com.ruoyi.factorydata.service.ICustomerLevelDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户等级数据Controller
 *
 * @author ruoyi
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/customerleveldata/customerleveldata")
public class CustomerLevelDataController extends BaseController {
    @Autowired
    private ICustomerLevelDataService customerLevelDataService;

    /**
     * 查询客户等级数据列表
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerLevelData customerLevelData) {
        startPage();
        List<CustomerLevelData> list = customerLevelDataService.selectCustomerLevelDataList(customerLevelData);
        return getDataTable(list);
    }

    /**
     * 导出客户等级数据列表
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:export')")
    @Log(title = "客户等级数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerLevelData customerLevelData) {
        List<CustomerLevelData> list = customerLevelDataService.selectCustomerLevelDataList(customerLevelData);
        ExcelUtil<CustomerLevelData> util = new ExcelUtil<CustomerLevelData>(CustomerLevelData.class);
        util.exportExcel(response, list, "客户等级数据数据");
    }

    /**
     * 获取客户等级数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerLevelDataService.selectCustomerLevelDataById(id));
    }

    /**
     * 新增客户等级数据
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:add')")
    @Log(title = "客户等级数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerLevelData customerLevelData) {
        boolean exists = customerLevelDataService.lambdaQuery().eq(CustomerLevelData::getLevelNumber, customerLevelData.getLevelNumber()).count() > 0;
        if (exists) {
            return error("客户等级已存在");
        }
        customerLevelData.setCreateBy(getUserId().toString());
        return toAjax(customerLevelDataService.insertCustomerLevelData(customerLevelData));
    }

    /**
     * 修改客户等级数据
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:edit')")
    @Log(title = "客户等级数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerLevelData customerLevelData) {
        boolean exists = customerLevelDataService.lambdaQuery()
                .ne(CustomerLevelData::getId, customerLevelData.getId())
                .eq(CustomerLevelData::getLevelNumber, customerLevelData.getLevelNumber())
                .count() > 0;
        if (exists) {
            return error("客户等级已存在");
        }
        return toAjax(customerLevelDataService.updateCustomerLevelData(customerLevelData));
    }

    /**
     * 删除客户等级数据
     */
    @PreAuthorize("@ss.hasPermi('customerleveldata:customerleveldata:remove')")
    @Log(title = "客户等级数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(customerLevelDataService.deleteCustomerLevelDataByIds(ids));
    }
}
